﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem
{
    public partial class RoleMangerWindow : ChildWindow
    {
        SystemService.SystemServiceClient Client = new SystemService.SystemServiceClient();
        SystemService.Role RoleModel = new SystemService.Role();
        public RoleMangerWindow()
        {
            InitializeComponent();
            this.Title = "新增角色";
            
            this.DataContext = RoleModel;


        }
        public RoleMangerWindow(SystemService.Role RoleModel1)
        {
            InitializeComponent();
            RoleModel = RoleModel1;

            this.Title = "编辑角色";
            
            this.DataContext = RoleModel;
        }
        private bool vidata()
        {
            if (string.IsNullOrEmpty(RoleModel.RoleName))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请输入角色名称！");
                return false;
            }
            return true;
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (vidata())
            {

                //编辑
                if (RoleModel.ID > 0)
                {
                    Client.UpdateRoleCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_UpdateRoleCompleted);
                    Client.UpdateRoleAsync(RoleModel);
                }
                else
                {
                    //新增

                    RoleModel.CreateTime = DateTime.Now;
                    Client.InsertRoleCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(Client_InsertRoleCompleted);
                    Client.InsertRoleAsync(RoleModel);
                }
                this.DialogResult = true;
            }
        }

        void Client_InsertRoleCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        void Client_UpdateRoleCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            //throw new NotImplementedException();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

